class GroupsController < ApplicationController
  before_filter :set_nav_session
  before_filter :login_required, :except => [:show, :index]
  
  def new
    @group = Group.new
  end
  
  def create
    @group = Group.new(params[:group])
    @group.creator = current_user
    if logged_in? && current_user.is_admin?
      @group.save!
      flash[:notice] = "创建了#{@group.title}小组"
    else
      flash[:notice] = "只有管理员才能创建"
    end
    redirect_back_or_default(groups_url)
  end
  
  def edit
    @group = Group.find(params[:id])
  end
  
  def update
    @group = Group.find(params[:id])
    @group.update_attributes!(params[:group])
    flash[:notice] = "你修改了#{@group.title}小组的资料"
    redirect_back_or_default(group_url(@group.id))
  end
  
  def show
    @group = Group.find(params[:id])
    @topics = @group.topics.paginate(:all, :include => :user, :page => params[:page] || 1, :order => "topics.updated_at desc")
  end
  
  def index
    @topics = Topic.paginate(:page => params[:page] || 1, :conditions => "topics.group_id in (3,4,5,6)", :include => :group, :order => "topics.updated_at desc")
  end
  
  protected
=begin
  def authorized?
    logged_in? && (current_user.is_admin? || current_user
  end
=end  
  def set_nav_session
    session[:nav] = "groups"
  end
end
